<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - loaders - MMD loader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #fff;
				color: #000;
				margin: 0px;
				overflow: hidden;
			}
			#info {
				color: #000;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				display:block;
			}
			#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
		</style>
	</head>

	<body>
		<div id="info">
		<a href="http://threejs.org" target="_blank">three.js</a> - MMDLoader test<br />
		<a href="https://github.com/mrdoob/three.js/tree/master/examples/models/mmd#readme" target="_blank">MMD Assets license</a><br />
		Copyright
		<a href="http://www.geocities.jp/higuchuu4/index_e.htm" target="_blank">Model Data</a>
		<a href="http://seiga.nicovideo.jp/seiga/im5162984" target="_blank">Pose Data</a>
		</div>

		<script src="../build/three.js"></script>

		<script src="js/libs/mmdparser.min.js"></script>
		<script src="js/libs/ammo.js"></script>

		<script src="js/loaders/TGALoader.js"></script>
		<script src="js/loaders/MMDLoader.js"></script>
		<script src="js/effects/OutlineEffect.js"></script>
		<script src="js/animation/CCDIKSolver.js"></script>
		<script src="js/animation/MMDPhysics.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>
		<script src="js/libs/dat.gui.min.js"></script>

		<script>

			var container, stats;

			var camera, scene, renderer, effect;
			var helper;

			var vpds = [];

			var ready = false;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var clock = new THREE.Clock();

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.z = 25;

				// scene

				scene = new THREE.Scene();

				var ambient = new THREE.AmbientLight( 0x666666 );
				scene.add( ambient );

				var directionalLight = new THREE.DirectionalLight( 0x887766 );
				directionalLight.position.set( -1, 1, 1 ).normalize();
				scene.add( directionalLight );

				//

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setClearColor( new THREE.Color( 0xffffff ) );
				container.appendChild( renderer.domElement );

				effect = new THREE.OutlineEffect( renderer );

				// model

				var onProgress = function ( xhr ) {
					if ( xhr.lengthComputable ) {
						var percentComplete = xhr.loaded / xhr.total * 100;
						console.log( Math.round(percentComplete, 2) + '% downloaded' );
					}
				};

				var onError = function ( xhr ) {
				};

				var modelFile = 'models/mmd/miku/miku_v2.pmd';
				var vpdFiles = [
					'models/mmd/vpds/01.vpd',
					'models/mmd/vpds/02.vpd',
					'models/mmd/vpds/03.vpd',
					'models/mmd/vpds/04.vpd',
					'models/mmd/vpds/05.vpd',
					'models/mmd/vpds/06.vpd',
					'models/mmd/vpds/07.vpd',
					'models/mmd/vpds/08.vpd',
					//'models/mmd/vpds/09.vpd',
					//'models/mmd/vpds/10.vpd',
					'models/mmd/vpds/11.vpd'
				];

				helper = new THREE.MMDHelper();

				var loader = new THREE.MMDLoader();

				loader.loadModel( modelFile, function ( object ) {

					mesh = object;

					mesh.position.y = -10;

					scene.add( mesh );

					var vpdIndex = 0;
					function loadVpd () {

						var vpdFile = vpdFiles[ vpdIndex ];

						loader.loadVpd( vpdFile, function ( vpd ) {

							vpds.push( vpd );

							vpdIndex++;

							if ( vpdIndex < vpdFiles.length ) {

								loadVpd();

							} else {

								initGui( mesh, vpds );
								ready = true;

							}

						}, onProgress, onError );

					}
					loadVpd();

				}, onProgress, onError );

				document.addEventListener( 'mousemove', onDocumentMouseMove, false );

				//

				window.addEventListener( 'resize', onWindowResize, false );

				function initGui () {

					var gui = new dat.GUI();

					var dictionary = mesh.morphTargetDictionary;

					var controls = {};
					var keys = [];

					var poses = gui.addFolder( 'Poses' );
					var morphs = gui.addFolder( 'Morphs' );

					function getBaseName ( s ) {

						return s.slice( s.lastIndexOf( '/' ) + 1 );

					}

					function initControls () {

						for ( var key in dictionary ) {

							controls[ key ] = 0.0;

						}

						controls.pose = -1;

						for ( var i = 0; i < vpdFiles.length; i++ ) {

							controls[ getBaseName( vpdFiles[ i ] ) ] = false;

						}

					}

					function initKeys () {

						for ( var key in dictionary ) {

							keys.push( key );

						}

					}

					function initPoses () {

						var files = { default: -1 };

						for ( var i = 0; i < vpdFiles.length; i++ ) {

							files[ getBaseName( vpdFiles[ i ] ) ] = i;

						}

						poses.add( controls, 'pose', files ).onChange( onChangePose );

					}

					function initMorphs () {

						for ( var key in dictionary ) {

							morphs.add( controls, key, 0.0, 1.0, 0.01 ).onChange( onChangeMorph );

						}

					}

					function onChangeMorph () {

						for ( var i = 0; i < keys.length; i++ ) {

							var key = keys[ i ];
							var value = controls[ key ];
							mesh.morphTargetInfluences[ i ] = value;

						}

					}

					function onChangePose () {

						var index = parseInt( controls.pose );

						if ( index === -1 ) {

							mesh.pose();

						} else {

							helper.poseAsVpd( mesh, vpds[ index ] );

						}

					}

					initControls();
					initKeys();
					initPoses();
					initMorphs();

					onChangeMorph();
					onChangePose();

					poses.open();
					morphs.open();

				}


			}

			function onWindowResize() {

				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				effect.setSize( window.innerWidth, window.innerHeight );

			}

			function onDocumentMouseMove( event ) {

				mouseX = ( event.clientX - windowHalfX ) / 2;
				mouseY = ( event.clientY - windowHalfY ) / 2;

			}

			//

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {
/*
				camera.position.x += ( - mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				camera.lookAt( scene.position );
*/
				effect.render( scene, camera );

			}

		</script>

	</body>
</html>
